package model

import (
	"fmt"
	"lutai/src/schema"
	"lutai/src/service/db"
)

// Test ...
type Test struct {
	db *db.DB
}

// Init 初始化
func (a *Test) Init(db *db.DB) *Test {
	db.AddTableWithName(schema.Test{}, schema.TestTableName)
	a.db = db
	return a
}

// Create 创建
func (a *Test) Create(t *schema.Test) error {
	return a.db.Insert(t)
}

// Update 更新
func (a *Test) Update(id string, info map[string]interface{}) error {
	_, err := a.db.UpdateByPK(schema.TestTableName, db.M{"RecordID": id}, info)
	return err
}

// Delete 删除
func (a *Test) Delete(id string) error {
	_, err := a.db.DeleteByPK(schema.TestTableName, db.M{"RecordID": id})
	return err
}

// Get 获取单条
func (a *Test) Get(id string) (*schema.Test, error) {
	var test schema.Test
	err := a.db.SelectOne(&test, fmt.Sprintf("select * from %s where RecordID=?", schema.TestTableName), id)
	return &test, err
}

// Query 查询多条
func (a *Test) Query() ([]*schema.Test, error) {
	var tests []*schema.Test
	_, err := a.db.Select(&tests, fmt.Sprintf("select * from %s", schema.TestTableName))
	return tests, err
}
